package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Prod;
import com.powernode.model.Result;
import com.powernode.service.ProdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RenBoQing
 * @date 2024年07月28日 11:37
 * @Description 商品控制器
 */
@Api(tags = "商品接口管理")
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProdService prodService;

    @ApiOperation("多条件分页查询商品信息")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public Result<Page<Prod>> loadProductPage(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String prodName,
            @RequestParam(required = false) Integer status) {
        Page<Prod> page = new Page<>(current, size);
        // 多条件 分页查询
        page = prodService.page(page, new LambdaQueryWrapper<Prod>()
                .like(prodName != null, Prod::getProdName, prodName)
                .eq(status != null, Prod::getStatus, status)
                .orderByDesc(Prod::getCreateTime)
        );
        // 返回数据
        return Result.success(page);
    }
    @ApiOperation("新增商品")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public Result<String> saveProduct(@RequestBody Prod product) {
    boolean success =  prodService.saveProduct(product);
        return Result.handle(success);
    }
    @ApiOperation("根据id查询商品的详情")
    @GetMapping("/info/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:info')")
    public Result<Prod> getProductById(@PathVariable Long prodId) {
        Prod product = prodService.queryProductInfoByProId(prodId);
        return Result.success(product);
    }
    @ApiOperation("修改商品信息")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prod:update')")
    public Result<String> updateProduct(@RequestBody Prod product) {
        boolean success = prodService.updateProductById(product);
        return Result.handle(success);
    }

    @ApiOperation("删除商品信息")
    @DeleteMapping("/delete/{prodId}")
    @PreAuthorize("hasAuthority('prod:prod:delete')")
    public Result<String> deleteProduct(@PathVariable Long prodId) {
        boolean success = prodService.removeProdById(prodId);
        return Result.handle(success);
    }

    //    ---------------------------- feign的接口 -----------------------------------
    @GetMapping("/getProdListByIds")
    Result<List<Prod>> getProdListByIds(@RequestParam("prodIds") List<Long> prodIds) {
        return Result.success(prodService.listByIds(prodIds));
    }
}
